<template>
    <div>
        <Tabs type="card" :value="selectPane" @on-click="clickTabPane">
            <TabPane label="外科列表" name="1">
                <div style="padding: 10px">
                    <iview-table
                            ref="table"
                            :span1="18"
                            :span2="6"
                            :baseURL="queryModel.baseURL"
                            :menuId="dataModel.menuId"
                            :columns="dataModel.columns"
                            :order="dataModel.order"
                            :pageNumber="pageModel.pageNumber"
                            :pageSize="pageModel.pageSize"
                            :defaultFilters="queryModel.defaultFilters"
                            v-on="{changeTotalSize:changeTotalSize,events:events,changeSelect:changeSelect}">
                    </iview-table>
                    <br>
                    <iview-page
                            style="margin-bottom: 200px"
                            :totalSize="pageModel.totalSize"
                            :pageNumber="pageModel.pageNumber"
                            :pageSize="pageModel.pageSize"
                            :pageSizeOpts="pageModel.pageSizeOpts"
                            v-on="{changePageNumber:changePageNumber,changePageSize:changePageSize}">
                    </iview-page>
                </div>
            </TabPane>
        </Tabs>
        <Modal v-model="showModal" ok-text="保存" cancel-text="取消" :closable='false' :mask-closable=true :width="1000">
            <h3 slot="header" style="color:#2D8CF0">选择外科</h3>
            <Tabs type="card">
                <TabPane label="请选择">
                    <surgical-project-list ref="surgical" v-on="{selectSurgical:selectSurgical}"></surgical-project-list>
                </TabPane>
                <TabPane :label="label">
                    <Table border :columns="columns1" :data="data1"></Table>
                </TabPane>
                <Button type="primary" @click="submit" style="height: 30px" slot="extra">保存</Button>
            </Tabs>
            <div slot="footer">
            </div>
        </Modal>
    </div>
</template>

<script>
    import iviewTable from '@/view/common/table/Table.vue';
    import iviewPage from '@/view/common/page/Page.vue';
    import surgicalProjectList from '@/view/business/medical/list/SurgicalProjectList.vue';
    export default {
        components: {
            iviewTable,
            iviewPage,
            surgicalProjectList
        },
        data () {
            return {
                showModal: false,
                label: (h) => {
                    return h('div', [
                        h('span', '已选择'),
                        h('Badge', {
                            props: {
                                count: this.data1.length
                            }
                        })
                    ])
                },
                selectPane: '1',
                selectRows: [],
                baseURL: '/user/treat/plan/surgical',
                queryModel: {
                    // 网关代理路径(必须)+资源路径(可选)
                    baseURL: '/user/treat/plan/surgical',
                    defaultFilters: [
                        {'field': 'complaint_id', 'value1': '-1', 'operate': '=='}
                    ]
                },
                dataModel: {
                    menuId: 682,
                    columns: [
                        {
                            type: 'selection',
                            width: 60,
                            align: 'center'
                        },
                        {
                            title: '项目名称',
                            key: 'projectName',
                        },
                        {
                            title: '单价',
                            key: 'price',
                        },
                        {
                            title: '数量',
                            key: 'quantity',
                        },
                        {
                            title: '总价',
                            key: 'totalPrice',
                        },
                        {
                            title: '收费状态',
                            key: 'chargeStatus',
                            render: (h, params) => {
                                let temp = params.row.chargeStatus
                                if (temp === 0) {
                                    return h('div', [
                                        h('Tag', {
                                            props: {
                                                type: 'border',
                                                color: 'yellow'
                                            },
                                            style: {
                                            }
                                        }, '待交费')
                                    ]);
                                } else if (temp === 1) {
                                    return h('div', [
                                        h('Tag', {
                                            props: {
                                                type: 'border',
                                                color: 'green'
                                            },
                                            style: {
                                            }
                                        }, '已交费')
                                    ]);
                                }
                            }
                        },
                        {
                            title: '操作',
                            width: 150,
                            align: 'center',
                            render: (h, params) => {
                                return h('div', [
                                    h('Button', {
                                        props: {
                                            type: 'error',
                                            size: 'default'
                                        },
                                        style: {
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: () => {
                                                this.delete(params.row)
                                            }
                                        }
                                    }, '删除')
                                ]);
                            }
                        }
                    ],
                    order: 'id desc'
                },
                pageModel: {
                    totalSize: 0,
                    pageNumber: 1,
                    pageSize: 10,
                    pageSizeOpts: [5, 10, 20, 50, 100]
                },
                data1: [],
                columns1: [
                    {
                        title: '项目名称',
                        key: 'name',
                    },
                    {
                        title: '数量',
                        key: 'quantity',
                        width: 150,
                        render: (h, params) => {
                            var vm = this
                            return h('InputNumber',{
                                props: {
                                    min: 1,
                                    value: params.row.quantity
                                },
                                style: {
                                    width: '100%'
                                },
                                on: {
                                    'on-change' (value) {
                                        params.row.quantity = value
                                        vm.data1[params.index] = params.row
                                    }
                                }
                            });
                        }
                    },
                    {
                        title: '操作',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'error',
                                        size: 'default'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.remove(params.index, params.row)
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }
                ]
            };
        },
        methods: {
            events (item) {
                if (item.path === 'add') {
                    this.add()
                }
            },
            add () {
                this.data1 = []
                this.$refs.surgical.refresh()
                this.showModal = true
            },
            delete (row) {
                this.$Modal.confirm({
                    title: '操作提示',
                    content: '<p>确认删除当前数据?</p>',
                    onOk: () => {
                        this.doDelete(row.id)
                    }
                })
            },
            doDelete (id) {
                let url = this.baseURL + '/deleteById/' + id
                this.$ajax.post(url).then(response => {
                    let {code, msg} = response.data
                    if (code === 0) {
                        this.$bus.alert('success', '删除成功')
                        this.refresh()
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            clickTabPane (name) {
                if (name === '1') {
                    this.$refs.table.refresh()
                } else if (name === '2') {
                    this.$refs.surgical.refresh()
                }
            },
            refresh () {
                this.queryModel.defaultFilters[0] = {'field': 'complaint_id', 'value1': this.$store.state.app.complaintId, 'operate': '=='}
                this.$refs.table.refresh()
            },
            selectSurgical (row) {
                row.projectId = row.id
                row.projectName = row.name
                row.patientId = this.$store.state.app.patientId
                row.complaintId = this.$store.state.app.complaintId
                row.quantity = 1
                this.data1.push(row)
            },
            remove (index, row) {
                this.data1.splice(index, 1);
            },
            submit () {
                if (this.data1.length > 0) {
                    let url = this.baseURL + '/saveOrUpdate'
                    this.$ajax.post(url, this.data1).then(response => {
                        let {code, msg} = response.data
                        if (code === 0) {
                            this.$bus.alert('success', '保存成功')
                            this.showModal = false
                            this.refresh()
                        } else {
                            this.$bus.alert('error', msg)
                        }
                    })
                } else {
                    this.$bus.alert('warning', '你没有选择任何外科项目')
                }
            },
            changeSelect (rows) {
                this.selectRows = rows
            },
            changeTotalSize (totalSize) {
                this.pageModel.totalSize = totalSize
            },
            changePageNumber (pageNumber) {
                this.pageModel.pageNumber = pageNumber
            },
            changePageSize (pageSize) {
                this.pageModel.pageSize = pageSize
            }
        },
        beforeRouteEnter (to, from, next) {
            next(vm => {
                vm.init()
            })
        }
    }
</script>

<style>
</style>
